package com.baymax.study.tuLing.netty.d1_base;

import io.netty.bootstrap.Bootstrap;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.DefaultEventLoop;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringEncoder;
import lombok.extern.slf4j.Slf4j;

import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;

/**
 * @author baymax
 * @since 2025-06-27
 */
@Slf4j
public class Demo2 {
    public static final int PORT = 60001;

    static class NettyServer {
        public static void main(String[] args) throws InterruptedException {
            EventLoopGroup boss = new NioEventLoopGroup(1); // 只负责accept请求
            EventLoopGroup worker = new NioEventLoopGroup(2); // 只负责处理读写

            // 单独处理handler的线程
            EventLoopGroup processor = new DefaultEventLoop();
            System.out.println("服务器启动完成");
            new ServerBootstrap().group(boss, worker)
                .channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel ch) {
                        ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {
                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) {
                                ByteBuf buf = (ByteBuf) msg;
                                log.info("{}", buf.toString(StandardCharsets.UTF_8));
                                ctx.fireChannelRead(msg); // 将消息传递给下一个handler
                            }
                        });
                        ch.pipeline().addLast(processor, "handler1", new ChannelInboundHandlerAdapter() {
                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) {
                                ByteBuf buf = (ByteBuf) msg;
                                log.info("{}", buf.toString(StandardCharsets.UTF_8));
                            }
                        });
                    }
                })
                .bind(new InetSocketAddress(PORT))
                .sync()
                .channel()
                .closeFuture()
                .sync();
        }
    }

    static class NettyClient {
        public static void main(String[] args) throws InterruptedException {
            Bootstrap bootstrap = new Bootstrap();
            ChannelFuture future = bootstrap.group(new NioEventLoopGroup())
                .channel(NioSocketChannel.class)
                .handler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ch.pipeline().addLast(new StringEncoder(StandardCharsets.UTF_8));
                    }
                })
                .connect(new InetSocketAddress("127.0.0.1", PORT))
                .sync();
            Channel channel = future.channel();
            channel.writeAndFlush("hello, netty");
            log.info("发送完成");
            channel.close();
        }
    }

}
